const msg = require('../database/tables/msg')
const { sequelize } = require('../database/init')
const { Op } = require('sequelize')
const { formatBody } = require('../tools')

module.exports = {
  /**
   * 清除聊天记录
   * @param ctx.time_limit 时间戳
   */
  async clear (ctx) {
    let { time_limit } = ctx.request.body
    time_limit = time_limit || 1000 * 60 * 60 * 24 * 5
    const limitDate = new Date(new Date().getTime() - time_limit)
    console.log('limitDate=>', limitDate)
    await msg.destroy({
      where: {
        createdAt: {
          [Op.lt]: limitDate
        }
      }
    })
    return ctx.body = formatBody(1, 'ok')
  }
}
